package br.com.ecomm.model.dao.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import br.com.ecomm.exception.ClienteException;
import br.com.ecomm.model.dao.ClienteDAO;
import br.com.ecomm.model.entity.Cliente;
import br.com.ecomm.util.jdbc.ConnectionJDBCFactory;
import br.com.ecomm.util.jdbc.ConnectionJDBCFactory.TypesConnections;
import br.com.ecomm.util.jdbc.jdbcImpl.ConnSingle;

public class ClienteDAOImpl implements ClienteDAO{
	
	private Connection conn;
	
	public ClienteDAOImpl() {
		conn = ConnSingle.getConnection(); 
	}
	
	@Override
	public boolean addCliente(Cliente cliente){
		
		try(PreparedStatement stmt = conn.prepareCall("INSERT INTO CLIENTE VALUES(?,?,?,?) ")){
			stmt.setInt(1, cliente.getAcc_id());
			stmt.setString(2,cliente.getFirstName());
			stmt.setString(3,cliente.getLastName());
			stmt.setString(4,cliente.getEmail());
			
			if(stmt.executeUpdate() > 0){
				return true;
			}
			
		}catch(SQLException e){
			
			throw new ClienteException(ClienteException.ERROR_INSERT, e); 
		}
		
		return false;
	}
	
	@Override
	public boolean deleteCliente(Cliente cliente) {
		
		try(PreparedStatement stmt = conn.prepareCall("DELETE CLIENTE WHERE ACC_ID = ?")){
			stmt.setInt(1, cliente.getAcc_id());
			
			if(stmt.executeUpdate() > 0){
				return true;
			}
			
		}catch(SQLException e){
			
			throw new ClienteException(ClienteException.ERROR_INSERT, e); 
		}
		
		return false;
	}
	
	
	@Override
	public boolean updateCliente(Cliente cliente) {
		if(deleteCliente(cliente)){
			return addCliente(cliente);
		}else{
			return false;
		}
		
	}

}
